#!/usr/bin/env sh

day_temperature=6500
night_temperature=4500

# 获取当前时区
timezone=$(timedatectl | grep "Time zone" | awk '{print $3}')

# 查找时区对应的经纬度
coords=$(grep "$timezone" /usr/share/zoneinfo/zone.tab | awk '{print $2}')

# 检查是否成功获取坐标
if [ -z "$coords" ]; then
  echo "ERROR: $timezone is not found"
  exit 1
fi

# 解析经纬度（保留符号）
latitude=$(echo "$coords" | awk -F '[+-]' '{print $2}')
longitude=$(echo "$coords" | awk -F '[+-]' '{print $3}')

# 提取符号
signs=$(echo "$coords" | sed 's/[^+-]//g')

# 添加符号
latitude=$(echo $signs | cut -c 1)$(echo $latitude | sed -E 's/([0-9]*)([0-9]{2})/\1.\2/')
longitude=$(echo $signs | cut -c 2)$(echo $longitude | sed -E 's/([0-9]*)([0-9]{2})/\1.\2/')


# 检查坐标格式是否正确
if [[ ! "$latitude" =~ ^[+-]?[0-9]+(\.[0-9]+)?$ ]] || [[ ! "$longitude" =~ ^[+-]?[0-9]+(\.[0-9]+)?$ ]]; then
  echo "ERROR: Wrong format latitude=$latitude, longitude=$longitude"
  exit 1
fi

# 启动 wlsunset
# wlsunset -l "$latitude" -L "$longitude" -t "$night_temperature" -T "$day_temperature"
gammastep -l "$latitude":"$longitude" -vr
